<%@ page import="java.util.regex.Pattern" %><%--
  Created by IntelliJ IDEA.
  User: 21553
  Date: 2021/10/31
  Time: 14:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");

//    获取参数
    String num1 = request.getParameter("num1");
    String num2 = request.getParameter("num2");
    String fu = request.getParameter("fu");

//    正则匹配
    Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");

    if (pattern.matcher(num1).matches()!=true || pattern.matcher(num2).matches()!=true){%>
        <jsp:forward page="calculator.jsp">
            <jsp:param name="no" value="不要输入字符!!!"/>
        </jsp:forward>
<%}
//    类型转换
    double num01 = Double.parseDouble(num1);
    double num02 = Double.parseDouble(num2);
    if(fu.equals("+")){
        double jie = num01+num02;
%>
        <jsp:forward page="calculator.jsp">
            <jsp:param name="jie" value="<%=jie%>"/>
            <jsp:param name="num01" value="<%=num1%>"/>
            <jsp:param name="num02" value="<%=num2%>"/>
        </jsp:forward>
    <%}
    if (fu.equals("-")){
        double jie = num01-num02;
        %>
        <jsp:forward page="calculator.jsp">
            <jsp:param name="jie" value="<%=jie%>"/>
            <jsp:param name="num01" value="<%=num1%>"/>
            <jsp:param name="num02" value="<%=num2%>"/>
        </jsp:forward>
<%}
        if (fu.equals("*")){
            double jie = num01*num02;
    %>
        <jsp:forward page="calculator.jsp">
            <jsp:param name="jie" value="<%=jie%>"/>
            <jsp:param name="num01" value="<%=num1%>"/>
            <jsp:param name="num02" value="<%=num2%>"/>
        </jsp:forward>
    <%}
        if (fu.equals("/")){
            if (num02==0){%>
            <jsp:forward page="calculator.jsp">
                <jsp:param name="no" value="0不能作为除数"/>
            </jsp:forward>
            <%}
            double jie = num01/num02;
    %>
        <jsp:forward page="calculator.jsp">
            <jsp:param name="jie" value="<%=jie%>"/>
            <jsp:param name="num01" value="<%=num1%>"/>
            <jsp:param name="num02" value="<%=num2%>"/>
        </jsp:forward>
<%}















    %>
</body>
</html>
